package com.hzit.hzb.core.controller.admin;


import com.hzit.common.exception.Assert;
import com.hzit.common.exception.BusinessException;
import com.hzit.common.result.R;
import com.hzit.common.result.ResponseEnum;
import com.hzit.hzb.core.pojo.entity.IntegralGrade;
import com.hzit.hzb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author hbxfwf
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级接口")
public class AdminIntegralGradeController {
	@Resource
	private IntegralGradeService integralGradeService;
	@GetMapping("/list")
	@ApiOperation(value = "积分等级列表")
	public R listAll(){
		return R.ok().data("list",integralGradeService.list());
	}

	@ApiOperation("删除积分等级")
	@DeleteMapping("removeById/{id}")
	public R removeById(@PathVariable("id")
						@ApiParam(value = "积分Id",required = true)
			            Long id){
		integralGradeService.removeById(id);
		return R.ok().message("删除积分等级成功!");
	}

	@ApiOperation("根据积分id查询积分等级")
	@GetMapping("findById/{id}")
	public R findById(@PathVariable("id")
                      @ApiParam(value="积分id",required = true)
                      Long id){
		IntegralGrade integralGrade = integralGradeService.getById(id);
		return R.ok().data("integralGrade",integralGrade);
	}

	@ApiOperation("新增积分等级")
	@PostMapping("/add")
	public R save(
			@ApiParam(value = "积分等级对象", required = true)
			@RequestBody IntegralGrade integralGrade){
		//如果借款额度为空就手动抛出一个自定义的异常！
		// if(integralGrade.getBorrowAmount() == null){
		// 	//BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
		// 	throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
		// }
		Assert.isTrue(integralGrade.getBorrowAmount() != null,ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
		boolean result = integralGradeService.save(integralGrade);
		if (result) {
			return R.ok().message("添加积分成功");
		} else {
			return R.error().message("添加积分失败");
		}
	}

	@ApiOperation("修改积分等级")
	@PutMapping("/update")
	public R update(
			@ApiParam(value = "积分等级对象", required = true)
			@RequestBody IntegralGrade integralGrade){
		//如果借款额度为空就手动抛出一个自定义的异常！
		// if(integralGrade.getBorrowAmount() == null){
		// 	//BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
		// 	throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
		// }
		Assert.isTrue(integralGrade.getBorrowAmount() != null,ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
		boolean result = integralGradeService.updateById(integralGrade);

		if (result) {
			return R.ok().message("修改积分成功");
		} else {
			return R.error().message("修改积分失败");
		}
	}
}

